package com.kwin.demo.server;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.WebApplicationType;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;

/**
 * @author kwin
 * @Date 2022/8/25 13:40
 **/
public class SpringBootApplicationStarter {
    private static ConfigurableApplicationContext applicationContext;

    public static synchronized ApplicationContext run(Class<?> clazz, String[] args) {
        if (applicationContext == null) {
            SpringApplicationBuilder springApplicationBuilder = new SpringApplicationBuilder(clazz);
            springApplicationBuilder.sources(clazz).web(WebApplicationType.NONE);
            SpringApplication springBootApplication = springApplicationBuilder.build();
            applicationContext = springBootApplication.run(args);
        }
        return applicationContext;
    }
}
